package com.haoma.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoma.dto.LabelDto;
import com.haoma.entity.Label;
import com.haoma.exception.BlogException;
import com.haoma.mapper.LabelMapper;
import com.haoma.result.Result;
import com.haoma.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Description: 标签相关的业务
 */
@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements LabelService {

    @Autowired
    private LabelMapper labelMapper;

    /**
     * 标签列表分页查询
     *
     * @param page  页码
     * @param limit 分页数量
     * @param name  分页条件参数
     * @return
     */
    public IPage pageLabelList(Long page, Long limit, String name) {
        // 创建分页对象
        Page<Label> labelPage = new Page<>(page, limit);
        // 设置分页条件
        LambdaQueryWrapper<Label> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            // 输入了关键字
            labelLambdaQueryWrapper.like(Label::getLabelName, name);
        }
        // 按修改时间排序
        labelLambdaQueryWrapper.orderByDesc(Label::getUpdateTime);
        // 开始分页查询
        return labelMapper.selectPage(labelPage, labelLambdaQueryWrapper);
    }

    /**
     * 新增标签
     *
     * @param labelName
     * @param color
     * @return
     */
    public Result addLabel(String labelName, String color) {
        Label label = new Label();
        if (!StringUtils.hasText(labelName)) {
            return Result.error("请输入正确的标签!");
        }
        label.setLabelName(labelName);
        label.setBtColor(color);
        try {
            labelMapper.insert(label);
        } catch (RuntimeException e) {
            throw new BlogException("标签已添加");
        }
        return Result.success();
    }

    /**
     * 获取标签
     *
     * @param id
     * @return
     */
    public Result getLabel(Integer id) {
        LambdaQueryWrapper<Label> lambdaQueryWrapper = new LambdaQueryWrapper<Label>().eq(Label::getId, id);
        Label label = labelMapper.selectOne(lambdaQueryWrapper);
        return Result.success(label);
    }

    /**
     * 修改标签
     *
     * @param labelName
     * @param color
     * @return
     */
    public Result update(Integer id, String labelName, String color) {
        if (!StringUtils.hasText(labelName)) {
            return Result.error("请输入正确的标签!");
        }
        LambdaQueryWrapper<Label> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Label::getId, id);
        Label label = labelMapper.selectOne(lambdaQueryWrapper);
        if (label == null) {
            return Result.error("标签有误!");
        }
        label.setLabelName(labelName);
        label.setBtColor(color);
        labelMapper.update(label, lambdaQueryWrapper);
        return Result.success();
    }


    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    public Result delete(Integer id) {
        Label byId = getById(id);
        if (byId.getLabelCount() > 0) {
            throw new BlogException("该标签下还有数据");
        }
        LambdaQueryWrapper<Label> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Label::getId, id);
        try {
            labelMapper.delete(lambdaQueryWrapper);
        } catch (RuntimeException runtimeException) {
            throw new BlogException("删除失败");
        }
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param labelDto
     * @return
     */
    public Result batchDelLabel(LabelDto labelDto) {
        List items = labelDto.getItems();
        if (CollectionUtils.isEmpty(items)) {
            return Result.error("删除失败!");
        }
        labelMapper.deleteBatchIds(items);
        return Result.success();
    }
}
